package com.example.service.impl;


import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    UserMapper userMapper;


    // redis注入
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User getUserByCookie(String ticket, HttpServletRequest request, HttpServletResponse response) {

        System.out.println("ticket===========" + ticket);
        //User user1 =(User)redisTemplate.opsForValue().get(ticket);
        //Object obj=redisTemplate.opsForValue().get(ticket);
        String str = (String) redisTemplate.opsForValue().get(ticket);

        System.out.println("1===========");
        User user = null;
        try {
            user = (User) new ObjectMapper().readValue(str, User.class);
            System.out.println("-----obj-------------" + user);
        } catch (JsonMappingException e) {
            // e.printStackTrace();
            System.out.println("exception 1 ===========" + e.toString());
        } catch (JsonProcessingException e) {
            // e.printStackTrace();
            System.out.println("exception 2 ===========" + e.toString());
        } catch (Exception e) {
            System.out.println("exception 3 ===========" + e.toString());
        }

        System.out.println("2===========");
        if (user == null) {
            System.out.println("user  为空===========");
        } else {
            System.out.println("user  不为空===========");
        }
        //   User user=(User) request.getSession().getAttribute(ticket);
//        User user=new User();
//        user.setID(1);
//        user.setUserName("zhangsan");
//        user.setName("zhangsan");
//        user.setStatue(true);
//        user.setTel("13382451617");


        return user;
    }

    @Override
    public List<User> checkUserService(Map<String, Object> params) {
        return userMapper.checkUser(params);
    }

    @Override
    public void deleteUserService(Map<String, Object> params) {
        userMapper.deleteUser(params);
    }

    @Override
    public void insertUserService(Map<String, Object> params) {
        userMapper.insertUser(params);
    }

    @Override
    public void modUserStateService(Map<String, Object> params) {
        userMapper.modUserState(params);
    }


}
